/*
 * (C) Copyright 2013 George Daswani (georgedaswani@gmail.com) and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 2.1 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl-2.1.html
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * Contributors:
 *    George Daswani (georgedaswani@gmail.com)
 */

package am.api.osgi;

import javax.transaction.xa.XAResource;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.log4j.Logger;

import am.api.AMConnection;
import am.api.XAAMConnection;
import am.api.XAAMConnectionFactory;
import am.api.exception.AMConnectionException;
import am.api.model.AMCredential;
import am.api.util.NLS;
import am.api.wrapper.ConnectionPool;
import am.api.wrapper.LocalXAResource;
import am.api.wrapper.XAAMConnectionImpl;

public class XAPooledAMConnectionFactory extends ConnectionPool implements
		XAAMConnectionFactory {

	private final static Logger logger = Logger
			.getLogger(XAPooledAMConnectionFactory.class);

	private final static long serialVersionUID = 1L;

	public XAPooledAMConnectionFactory() {
		super();
	}

	@Override
	public AMConnection getConnection(AMCredential credential)
			throws AMConnectionException {

		AMConnection connection = null;

		try {
			connection = super.getConnection(credential);
		} catch (Exception error) {
			AMConnectionException connE = new AMConnectionException(
					NLS.ERRORS.getString("connection.failure"));
			connE.initCause(error);
			throw connE;
		}

		if (logger.isDebugEnabled()) {
			logger.debug(String.format("connection: [%s]", connection));
		}

		return connection;
	}

	private XAResource createXAResource(AMConnection connection) {
		return new LocalXAResource(connection);
	}

	@Override
	public AMConnection getConnection() throws AMConnectionException {
		return getConnection(null);
	}

	public XAAMConnection getXAConnection() throws AMConnectionException {

		AMConnection connection = getConnection();

		return new XAAMConnectionImpl(connection, createXAResource(connection));
	}

	public XAAMConnection getXAConnection(AMCredential credential)
			throws AMConnectionException {
		AMConnection connection = getConnection(credential);

		return new XAAMConnectionImpl(connection, createXAResource(connection));
	}

	public boolean equals(Object obj) {
		if (obj == null) {
			return false;
		}
		if (obj == this) {
			return true;
		}
		if (obj.getClass() != getClass()) {
			return false;
		}
		// XAPooledAMConnectionFactory rhs = (XAPooledAMConnectionFactory) obj;
		return new EqualsBuilder().appendSuper(super.equals(obj)).isEquals();
	}

	public int hashCode() {
		return new HashCodeBuilder(103, 105).appendSuper(super.hashCode())
				.toHashCode();
	}

	public void init() {
		super.init();
	}

	public void destroy() {
		super.destroy();
	}

	public AMCredential getDefaultCredential() {
		return super.getDefaultCredential();
	}

	public void setDefaultCredential(AMCredential defaultCredential) {
		super.setDefaultCredential(defaultCredential);
	}
}
